<?php

//政策库解读
class zhengcejieduController extends baseController
{

    //显示文章列表
    public function indexAction()
    {
        $table_id = $this->common->Get('table_id');
        if ($table_id == "")
            die("table_id 不能为空");
        $filedModel = new filedModel();
        $filedlistJson = $filedModel->getFiledJsonByTableid($table_id);

        $filedSearchList = $filedModel->getsearchfiledByTableid($table_id);
        //会显示出来的字段
        $filedShowList = $filedModel->getshowfiledByTableid($table_id);

        $tableModel = new tableModel();
        $tableDetail = $tableModel->findOne($table_id);

        $sortModel = new sortModel();
        $classidArray = $sortModel->getsortByclassid("");
        //挂载 TREE 类
        $this->helper('tree');
        $tree = new Tree($classidArray);
        $str = "<option value=\$id  \$selected >\$spacer\$name</option>";
        $html_option = '';
        $html_option .= $tree->get_tree($tableDetail['u10'], $str, -1);

        //挂载  组件类
        $this->library('Component');
        $component = new Component();


        include CUR_VIEW_PATH . "Szhengcejiedu" . DS . "article_list.html";
    }

    //查询数据列表
    public function gettablelistbytableidAction(){

        $tableModel = new tableModel();
        $table_id = $this->common->Get('table_id');
        $tableName = $tableModel->getTablenameByTableid($table_id);

        $_tableModel = new Model($tableName);

        $page = $this->common->Get("page");
        $limit= $this->common->Get("limit");
        $page = ($page-1)*$limit;
        $table_sql = $tableModel->getSqlWhereStr_new($table_id);
        if($table_sql == "")
        {
            $table_sql = " status = 1";         //只查询未删除数据
        }else{
            $table_sql .= " and status = 1";
        }
        $_tableList1 = $_tableModel->findBySql("select * from {$tableName} where {$table_sql} order by fawenriqi desc,id desc limit {$page},{$limit} ");
        //挂载控件类
        $this->library("Component");
        $ComponentClass = new Component();
        $filedModel=new filedModel();
        $filedAraay=$filedModel->getallFiledByTableid($table_id);
        foreach ($filedAraay as $v)
        {
            //处理不同字段的显示数据
            foreach ($_tableList1 as $k1=>$v1) {
                $v1[$v['u1']] =$ComponentClass->showKj($v['u7'],$v['u2'],$v['u1'] ,$v1[$v['u1']],$v['u3'] ,$v['id']) ;
                $_tableList1[$k1]=$v1;
            }
        }

        foreach ($_tableList1 as &$value){
            $value['sort_id'] = (new sortModel())->value(['id'=>$value['sort_id']])['u1'];
        }

        $_tableList["data"]=$_tableList1;
        $_tableList["code"]="0";
        $_tableList["msg"]="";
        $_tableList["count"]=count($_tableModel->findBySql("select * from {$tableName} where {$table_sql}  "));

        $this->common->ajaxReturn($_tableList);
    }

    //显示文章列表
    public function addAction()
    {
        $table_id = $this->common->Get('table_id');
        if ($table_id == "") {
            die("table_id 不能为空");
        }
        $filedModel = new filedModel();
        $filedList = $filedModel->getallFiledByTableid($table_id);
        $filedshowtypeList = $filedModel->getfiledShowTypeBytableid($table_id);

        $tableModel = new tableModel();
        $tableDetail = $tableModel->findOne($table_id);

        $sortModel = new sortModel();
        $classidArray = $sortModel->getsortByclassid("");
        //挂载 TREE 类
        $this->helper('tree');
        $tree = new Tree($classidArray);
        $str = "<option value=\$id  \$selected >\$spacer\$name</option>";
        $html_option = '';
        $html_option .= $tree->get_tree($tableDetail['u10'], $str, -1);

        //挂载  组件类
        $this->library('Component');
        $component = new Component();


        include CUR_VIEW_PATH . "Szhengcejiedu" . DS . "article_add.html";
    }

    //显示文章列表
    public function editAction()
    {
        $table_id = $this->common->Get('table_id');
        $id = $this->common->Get('id');
        if ($table_id == "") {
            die("table_id 不能为空");
        }
        $filedModel = new filedModel();
        $filedList = $filedModel->getallFiledByTableid($table_id);
        $filedshowtypeList = $filedModel->getfiledShowTypeBytableid($table_id);

        $tableModel = new tableModel();
        $tableDetail = $tableModel->findOne($table_id);
        $tablename = $tableModel->getTablenameByTableid($table_id);
        $_tableModel = new Model($tablename);
        $_tableDetail = $_tableModel->findOne($id);

        $sortModel = new sortModel();
        $classidArray = $sortModel->getsortByclassid("");
        //挂载 TREE 类
        $this->helper('tree');
        $tree = new Tree($classidArray);
        $str = "<option value=\$id  \$selected >\$spacer\$name</option>";
        $html_option = '';
        $html_option .= $tree->get_tree($tableDetail['u10'], $str, $_tableDetail['sort_id']);

        //挂载  组件类
        $this->library('Component');
        $component = new Component();


        include CUR_VIEW_PATH . "Szhengcejiedu" . DS . "article_edit.html";
    }

    // 定义insert方法，完成文章模型的插入
    public function insertAction()
    {
        //获取该用户组信息
        $data_return["status"] = false;
        $data_return["msg"] = "";
        $data_return["code"] = 1;

        $table_id = $this->common->Requert('table_id');
        // 获得当前表名
        $tableModel = new tableModel();
        $tableName = $tableModel->getTablenameByTableid($table_id);
        //先获取文章信息
        $_tableModel = new Model($tableName);

        //1.收集表单数据
        $data = $_tableModel->getFieldArray();

        //2.验证和处理

        $this->helper('input');
        $data = deepspecialchars($data);
        $data = deepslashes($data);


        $filedModel = new filedModel();
        $filedAraay = $filedModel->getallFiledByTableid($table_id);


        //处理表单验证===>正则匹配
        foreach ($filedAraay as $v) {
            //设置了不为空
            if ($v['u4'] == "是") {
                if ($data[$v['u1']] == "" || $data[$v['u1']] == null) {
                    $data_return["msg"] = "操作失败:" . $v['u2'] . "不能为空";
                    $this->common->ajaxReturn($data_return);

                }

            }

            if (isset($v['u14']) && $GLOBALS['config_regular'][$v['u14']] != "无" && $GLOBALS['config_regular'][$v['u14']] != "") {
                $n = preg_match($GLOBALS['config_regular'][$v['u14']], $data[$v['u1']], $array);
                /*接下来的正则表达式("/131,132,133,135,136,139开头随后跟着任意的8为数字 '|'(或者的意思)
                 * 151,152,153,156,158.159开头的跟着任意的8为数字
                 * 或者是188开头的再跟着任意的8为数字,匹配其中的任意一组就通过了
                 * /")*/
                //var_dump($array); //看看是不是找到了,如果找到了,就会输出电话号码的
                if (count($array) == 0) {
                    //改字段不合法
                    $data_return["msg"] = $v['u2'] . "字段不合法";
                    $this->common->ajaxReturn($data_return);

                }
            }
        }

        //单独处理时间
        foreach ($filedAraay as $v) {
            $filedList = $filedModel->findBySql("select * from sl_filed where  u7='时间框'  and model_id='{$table_id}' and u1='{$v['u1']}' ");

            if (count($filedList) > 0) {
                //and u8='CURRENT_TIMESTAMP'
                if ($filedList['u8'] == "CURRENT_TIMESTAMP") {
                    $data[$v['u1']] = date('Y-m-d H:i:s', time());
                } else if ($data[$v['u1']] == "") {
                    unset($data[$v['u1']]);
                }

            }

        }


        //单独处理密码
        foreach ($filedAraay as $v) {
            $filedList = $filedModel->findBySql("select * from sl_filed where  u7='密码' and model_id='{$table_id}' and u1='{$v['u1']}' ");
            if (count($filedList) > 0) {
                $data[$v['u1']] = md5($data[$v['u1']]);
            }
        }
        foreach ($filedAraay as $v) {
            $filedList = $filedModel->findBySql("select * from sl_filed where  u7='文本编辑器' and model_id='{$table_id}' and u1='{$v['u1']}' ");
            if (count($filedList) > 0 && !empty($data[$v['u1']])) {
                $data[$v['u1']] = html_entity_decode($data[$v['u1']]);
            }
        }


        //3调用模型完成入库并给出提示
        if ($_tableModel->insert($data)) {
            $data_return["status"] = true;
            $data_return["msg"] = "添加成功";
            $data_return["code"] = 0;

        } else {
            $data_return["msg"] = "添加失败";

        }
        $this->common->ajaxReturn($data_return);

    }

    // 定义insert方法，完成文章模型的插入
    public function updateAction()
    {
        //获取该用户组信息
        $data_return["status"] = false;
        $data_return["msg"] = "";
        $data_return["code"] = 1;

        $id = $this->common->Requert('id');
        $table_id = $this->common->Requert('table_id');
        if ($id == "") {
            $data_return["msg"] = "id 不能为空";
            $this->common->ajaxReturn($data_return);
        }
        // 获得当前表名
        $tableModel = new tableModel();
        $tableName = $tableModel->getTablenameByTableid($table_id);
        //先获取文章信息
        $_tableModel = new Model($tableName);

        //1.收集表单数据
        $data = $_tableModel->getFieldArray();

        //2.验证和处理

        $this->helper('input');
        $data = deepspecialchars($data);
        $data = deepslashes($data);


        $filedModel = new filedModel();
        $filedAraay = $filedModel->getallFiledByTableid($table_id);


        //处理表单验证===>正则匹配
        foreach ($filedAraay as $v) {
            //设置了不为空
            if ($v['u4'] == "是") {
                if ($data[$v['u1']] == "" || $data[$v['u1']] == null) {
                    $data_return["msg"] = "操作失败:" . $v['u2'] . "不能为空";
                    $this->common->ajaxReturn($data_return);

                }

            }

            if (isset($v['u14']) && $GLOBALS['config_regular'][$v['u14']] != "无" && $GLOBALS['config_regular'][$v['u14']] != "") {
                $n = preg_match($GLOBALS['config_regular'][$v['u14']], $data[$v['u1']], $array);
                /*接下来的正则表达式("/131,132,133,135,136,139开头随后跟着任意的8为数字 '|'(或者的意思)
                 * 151,152,153,156,158.159开头的跟着任意的8为数字
                 * 或者是188开头的再跟着任意的8为数字,匹配其中的任意一组就通过了
                 * /")*/
                //var_dump($array); //看看是不是找到了,如果找到了,就会输出电话号码的
                if (count($array) == 0) {
                    //改字段不合法
                    $data_return["msg"] = $v['u2'] . "字段不合法";
                    $this->common->ajaxReturn($data_return);

                }
            }
        }


        //单独处理时间
        foreach ($filedAraay as $v) {
            $filedList = $filedModel->findBySql("select * from sl_filed where  u7='时间框'  and model_id='{$table_id}' and u1='{$v['u1']}' ");

            if (count($filedList) > 0) {
                if ($data[$v['u1']] == "") {
                    unset($data[$v['u1']]);
                }

            }

        }


        //单独处理密码
        foreach ($filedAraay as $v) {
            $filedList = $filedModel->findBySql("select * from sl_filed where  u7='密码' and model_id='{$table_id}' and u1='{$v['u1']}' ");
            if (count($filedList) > 0 && $data[$v['u1']] != "") {
                $data[$v['u1']] = md5($data[$v['u1']]);
            }
        }
        foreach ($filedAraay as $v) {
            $filedList = $filedModel->findBySql("select * from sl_filed where  u7='文本编辑器' and model_id='{$table_id}' and u1='{$v['u1']}' ");
            if (count($filedList) > 0 && !empty($data[$v['u1']])) {
                $data[$v['u1']] = html_entity_decode($data[$v['u1']]);
            }
        }


        //数据修改为未同步
        $data["synchro_status"] = 1;

        //3调用模型完成入库并给出提示
        if ($_tableModel->update($data)) {
            $data_return["status"] = true;
            $data_return["msg"] = "更新成功";
            $data_return["code"] = 0;

        } else {
            $data_return["msg"] = "更新失败";

        }
        $this->common->ajaxReturn($data_return);

    }

    //定义delete方法，完成文章的删除
    public function deleteAction()
    {
        $data_return["status"] = true;
        $data_return["msg"] = "加入回收站成功";
        $data_return["code"] = 0;

        $ids = $this->common->Requert('id');
        $table_id = $this->common->Requert('table_id');
        if ($ids == "") {
            $data_return["msg"] = "id 不能为空";
            $this->common->ajaxReturn($data_return);
        }
        if ($table_id == "") {
            $data_return["msg"] = "table_id不能为空";
            $this->common->ajaxReturn($data_return);
        }

        $tableModel = new tableModel();
        $tableName = $tableModel->getTablenameByTableid($table_id);

        $array_id = explode(",", $ids);
        $array_id = array_unique($array_id);

        $_tableModel = new Model($tableName);

        //新增回收站数据
        $recycleModule = new recycleModule();
        foreach ($array_id as $val){
            $data = [
                "type" => $table_id,
                "data_id" => $val,
                "del_admin" => $_SESSION["admin"]["user_id"],
                "dtime" => date("Y-m-d H:i:s"),
            ];
            $recycleModule->insert($data);
        }

        //修改数据状态
        $data_update = ["status"=>2];
        $res = $_tableModel->where("id in ($ids)")->update($data_update);
        if ($res) {
            $data_return["status"] = true;
            $data_return["msg"] = "加入回收站成功";
            $data_return["code"] = 1;
        } else {
            $data_return["msg"] = "加入回收站失败";
        }
        $this->common->ajaxReturn($data_return);

    }

}